package org.pine.common.config;

import org.pine.common.utils.JsonUtil;
import org.pine.common.utils.RedisCache;
import org.redisson.api.RedissonClient;
import org.redisson.codec.JsonJacksonCodec;
import org.redisson.spring.cache.RedissonSpringCacheManager;
import org.redisson.spring.starter.RedissonAutoConfigurationCustomizer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.cache.CacheManager;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * 缓存配置
 *
 * @author pine manage
 * @since 2024-08-13
 */
@Configuration
public class CacheConfig {
    @Bean(name = "redissonCacheManager")
    @ConditionalOnMissingBean
    public CacheManager redissonCacheManager(@Autowired RedissonClient redissonClient) {
        // 可以自定义缓存配置
        return new RedissonSpringCacheManager(redissonClient);
    }

    @Bean
    @ConditionalOnMissingBean
    public RedisCache redisCache(@Autowired CacheManager redissonCacheManager) {
        return new RedisCache(redissonCacheManager);
    }


    @Bean
    public RedissonAutoConfigurationCustomizer redissonAutoConfigurationCustomizer() {
        // 指定缓存序列化方式
        JsonJacksonCodec jsonJacksonCodec = new JsonJacksonCodec(JsonUtil.getObjectMapper());
        return config -> config.setCodec(jsonJacksonCodec);
    }
}
